package com.dhemery.mack.adapters;

import org.eclipse.jface.resource.ImageDescriptor;

import com.dhemery.mack.model.Node;

public class NodeImageDescriptorFactory {
	private final ImageDescriptor nullImageDescriptor = ImageDescriptor.getMissingImageDescriptor();
	private final ImageDescriptorSource source;
	
	public NodeImageDescriptorFactory(ImageDescriptorSource source) {
		this.source = source;
	}

	public ImageDescriptor createImageDescriptor(Node node) {
		return createImageDescriptor(ImageKey.getImageKey(node));
	}

	public ImageDescriptor createImageDescriptor(ImageKey imageKey) {
		if(hasPath(imageKey)) return createImageDescriptorChecked(imageKey.getImagePath());
		return nullImageDescriptor;
	}

	private ImageDescriptor createImageDescriptorChecked(String imagePath) {
		ImageDescriptor descriptor = source.getImageDescriptor(imagePath);
		if(descriptor != null) return descriptor;
		return nullImageDescriptor;
	}

	private boolean hasPath(ImageKey imageKey) {
		return (imageKey != null) && (imageKey.getImagePath() != null);
	}
}
